<?php
class Cambodia extends SBKH{
	public function index($par = ""){
		$this -> SetLayout('province');		
		$obj = $this -> LoadModel('Cambodia');
		$records = $obj -> getProvinces();

		$this -> SetVar('records', $records);
		$this -> View('province');
	}
	public function provinces($proId = 0, $disId = 0, $depId = 0){
		$this -> SetLayout('district');
		$obj = $this -> LoadModel('Cambodia');
		
		if($disId == 0){ 
			// show all district by province id
			$records = $obj -> getDistricts($proId);
			$province = $obj -> getProvinces($proId);

			$this -> SetVar('province', $province[0]);
			$this -> SetVar('records', $records);
			$this -> View('district');
		}else{ 
			// show all depot optional by district id
			$this -> SetLayout('depot');
			
			$records = $obj -> getDepots($disId);
			$recordsDis = $obj -> getDistricts($proId);
			$province = $obj -> getProvinces($proId);
			$district = $obj -> getDistricts($proId, $disId);
			
			$this -> SetVar('province', $province[0]);
			$this -> SetVar('district', @$district[0]);
			$this -> SetVar('records', $records);
			$this -> SetVar('recordsDes', $recordsDis);
			$this -> View('depot');
		}
		
	}
	public function get_district($proId = 0){
		$this -> SetLayout(false);
		$obj = $this -> LoadModel('Cambodia');
		$disctricts = $obj -> getDistricts($proId);
		$html = "";
		if(count($disctricts)){
			$html = "
						<ul>
						";
			foreach($disctricts as $dis){
				$html .= "
						<li>" . $dis -> district_name . "</li>
						";
			}
			$html .= "</ul";
		}
		if($html == "") echo "There is no data.";
		else echo $html;
	}	
	/*
	 * Get all depot by district Id, when over on area map
	 * Param district id
	 * */
	public function get_depot($disId = 0){
		$this -> SetLayout(false);
		$obj = $this -> LoadModel('Cambodia');
		$disctricts = $obj -> getDepots($disId);
		$html = "";
		if(count($disctricts)){
			$html = "
						<ul>
						";
			foreach($disctricts as $dis){
				$html .= "
						<li>" . $dis -> depot_name . "</li>
						";
			}
			$html .= "</ul";
		}
		if($html == "") echo "There is no data.";
		else echo $html;
	}
	/*
	 * Get depots by province id
	 * param province id
	 */	
	public function get_depots_by_province(){
		$proId = GetURL('sb_proId');
		$proName = GetURL('sb_proName');
		$this -> SetLayout(false);
		$obj = $this -> LoadModel('Cambodia');
		$records = $obj -> getDepotsByProvince($proId);
		$html = '
		<table class="table table-striped table-bordered table-hover">
		<colgroup><col widht=""><col width=""><col width=""><col width=""><col width=""><col></colgroup>
		<thead>
		<tr>
			<th colspan="4">' . $proName . '</th>	
		</tr>
		<tr>
		<th>ដេប៉ូ</th>
		<th>ឡាន</th>
		<th>ធុង</th>
		<th>ចំនួនលក់</th>
		<!--<th>ភាករយ</th>
		<th>កំណត់ចំណាំ</th>-->
		</tr>
		</thead>
		<tbody>';
		$body = '';
		if(count($records)){
			foreach($records as $record){
				$body .= '<tr class="depot-info" data-placement="left" data-toggle="tooltip" title="Last Modified: ' . ($record -> modified . ' Modified By: ' . $record -> modified_by) . '">';
				$body .= '<td>' . $record -> depot_name . '</td>';
				$body .= '<td>' . $record -> truck_number . '</td>';
				$body .= '<td>' . $record -> tank_number . '</td>';
				$body .= '<td>' . $record -> amount_of_sell .'</td>';
				//$body .= '<td>' . $record -> percentage . '</td>';
				//$body .= '<td>' . $record -> note . '</td>';
				$body .= '</tr>';
			}
		}else{
			$body .= '<tr><td colspan="6" class="text-center">មិនមានដេប៉ូទេ</td></tr>';
		}
		$html .= $body . '
					</tbody>
				</table>';
		echo $html;
	}
}
?>